<?php

namespace Admin\Controller;

use Core\Controller\AdminBaseController;
use Core\Exception\BusinessException;
use Exception;
use Logger;
use Model\AlbumTable;
use Zend\View\Model\JsonModel;
use Zend\View\Model\ViewModel;
use Zend\View\View;


class RoleController extends AdminBaseController {

    public function indexAction() {

        $roleTable = $this->getTableFinder()->getRoleTable();
        $allRoles = $roleTable->getAll();

        $privilegeTable = $this->getTableFinder()->getPrivilegeTable();
        $allPrivileges = $privilegeTable->getAll();

        return new ViewModel(array('allRoles'=>$allRoles, 'allPrivileges'=>$allPrivileges));

    }

    public function deleteAction(){
        try{
            $id = $this->params()->fromPost('id');


            if(!$id){
                throw new BusinessException("Chưa truyền id để xóa ");
            }

            $roleTable = $this->getTableFinder()->getRoleTable();

            $roleTable->delete($id );
            return new JsonModel(array('success'=>true));
        }catch(Exception $e){
            $this->fireExceptionEvent($e, \Core\Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }


    public function saveAction(){
        try{

            $id = $this->params()->fromPost('id');
            $code = $this->params()->fromPost('code');
            $name = $this->params()->fromPost('name');
            $description = $this->params()->fromPost('description');
            $data = array("code"=>$code,"name"=>$name, "description"=>$description);

            $roleTable = $this->getTableFinder()->getRoleTable();

            if($id){
                $roleTable->update($data, $id);
            }
            else{
                $roleTable->insert($data);
            }
            return new JsonModel(array('success'=>true));
        }catch(Exception $e){
            $this->fireExceptionEvent($e, \Core\Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }




    public function roleCheckAllPrivilegeAction(){
        try{
            $roleId = $this->params()->fromPost('roleId');
            $isSelected = $this->params()->fromPost('isSelected');

            $roleTable = $this->getTableFinder()->getRoleTable();
            $role_Item = $roleTable->getRole($roleId);

            $role_Item->detachPrivileges();
            if($isSelected == 1){
                $privilegeTable = $this->getTableFinder()->getPrivilegeTable();

                $allPrivileges = $privilegeTable->getAll();
                foreach ($allPrivileges as $aPrivilege) {
                    $role_Item->updatePrivilege($aPrivilege->id, true);
                }
            }

            return new JsonModel(array('success'=>true));
        }catch(Exception $e){
            $this->fireExceptionEvent($e, \Core\Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }

    }

    public function roleCheckAPrivilegeAction(){
        try{
            $roleId = $this->params()->fromPost('roleId');
            $isSelected = $this->params()->fromPost('isSelected');
            $privilegeId = $this->params()->fromPost('privilegeId');
            $roleTable = $this->getTableFinder()->getRoleTable();
            $role_Item = $roleTable->getRole($roleId);

            if($isSelected == 1){
                $role_Item->updatePrivilege($privilegeId, true);
            }else{
                $role_Item->updatePrivilege($privilegeId, false);
            }

            return new JsonModel(array('success'=>true));
        }catch(Exception $e){
            $this->fireExceptionEvent($e, \Core\Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }

    }

    public function roleGetByIdAction(){
        try{

            $roleId = $this->params()->fromPost('id');
            $roleTable = $this->getTableFinder()->getRoleTable();
            $role_Item = $roleTable->getRole($roleId);

            return new JsonModel(array('success'=>true,
                'data'=>array('id'=>$role_Item->id, 'code'=>$role_Item->code,
                    'name'=>$role_Item->name, 'description'=>$role_Item->description)));

        }catch(Exception $e){
            $this->fireExceptionEvent($e, \Core\Constant::$EVENT_NAME_ADMIN_EXCEPTION);
            return $this->genJsonErrorView($e);
        }
    }


}